# Tiona Zuzul & Matthew Lee
# GOV 2001
# Mizruchi & Marquis (2005)
# APRIL 

#-----------------------------------------

# Load libraries

library(foreign)
library(Zelig)
library(sna)

# Load data

miz57 <-read.dta("miz57b.dta")
head(miz57)
ncol(miz57)


####REPLICATION
##Generating Descriptive Statistics
data <- miz57[,cbind(8,13,14,15,16,54,55,56,57,58)]
head(data)
C1 <- cor(data)
C1

##MODEL 1
#Create vector of Y - Similarity(dyadic)

Y <- as.matrix(miz57[,8])
summary(Y)

#Create matrix of X variables

Xp <- as.matrix(miz57[,cbind(13,14,15,16)])
X <- cbind(1,Xp)
colnames(X) <- c("constant","constraint","_5pctstock","direct_ilx","indirect_ilx")
head(X)

OLS <- lm(Y ~ X[,2] + X[,3] + X[,4] + X[,5])
summary(OLS)

####MODEL 3: Same Y, different X

Y <- as.matrix(miz57[,8])
summary(Y)

#Create matrix of X variables

X3 <- as.matrix(miz57[,cbind(55,56,57,58)])
head(X3)

OLS3 <- lm(Y ~ X3[,1] + X3[,2] + X3[,3] + X3[,4])
summary(OLS3)

####MODEL 2: Different Y, same X as 3

Y2 <- as.matrix(miz57[,54])
summary(Y2)

OLS2 <- lm(Y2 ~ X3[,1] + X3[,2] + X3[,3] + X3[,4])
summary(OLS2)

#####MODEL 4: Y1, all X

OLS4 <- lm(Y ~ X[,2] + X[,3] + X[,4] + X[,5] + X3[,1] + X3[,2] + X3[,3] + X3[,4])
summary(OLS4)


####MODEL 5: Y2, all x

OLS5 <- lm(Y2 ~ X[,2] + X[,3] + X[,4] + X[,5] + X3[,1] + X3[,2] + X3[,3] + X3[,4])
summary(OLS5)



#####EXTENSION
#Generate new X variable and descriptive statistics

newdata <- as.matrix(miz57[,cbind(8,13,14,15,16,59,60,61,62,36,37)])
head(newdata)
summary(newdata)
sd(newdata)
C2 <- cor(newdata)
C2

newY <- as.matrix(newdata[,1])
head(newY)

newX <- as.matrix(newdata[,-1])
head(newX)

#MODEL 6: new variables 

OLS6 <- lm(newY ~ newX[,1] + newX[,2] + newX[,3] + newX[,4] + newX[,5] + newX[,7]+ newX[,8])
summary(OLS6)

#MODEL 7: interactions
OLS7 <- lm(newY ~ newX[,1] + newX[,2] + newX[,3] + newX[,4] + newX[,5]*newX[,8] + newX[,7]*newX[,8])
summary(OLS7)


#MODEL 8: market concentration and interactions
OLS8 <- lm(newY ~ newX[,1] + newX[,2] + newX[,3] + newX[,4] + newX[,5]*newX[,8] + newX[,7]*newX[,8] + newX[,9]*newX[,8])
summary(OLS8)

head(newX)

####Model 7 using Zelig
data2 <- as.data.frame(newdata)
class(data2)
head(data2)

#change column names
coltitles <- data.frame(v1="sim", v2="constraint", v3="stock", v4="direct", v5="indirect", v6="bs", v7="ss", v8="bb", v9="ind", v10="ifour", v11="jfour")
names(data2)<-unlist(coltitles)
head(data2)

zelig7 <- zelig(sim ~ constraint + stock + direct + indirect + bs*ind + bb*ind, data=data2, model="ls")

zelig7


###First differences:
ss.same <- setx(zelig7, ind = 1, bb=0, bs=0)
ss.diff <- setx(zelig7, ind = 0, bb=0, bs=0)
out1 <- sim(zelig7, x = ss.same, x1 = ss.diff)
summary(out1)

bs.same <- setx(zelig7, ind = 1, bb=0, bs=1)
bs.diff <- setx(zelig7, ind = 0, bb=0, bs=1)
out2 <- sim(zelig7, x = bs.diff, x1 = bs.same)
summary(out2)

bb.same <- setx(zelig7, ind = 1, bb=1, bs=0)
bb.diff <- setx(zelig7, ind = 0, bb=1, bs=0)
out3 <- sim(zelig7, x = bb.same, x1 = bb.diff)
summary(out3)

#Compare with effects of original model:
ind.high <- setx(zelig7, indirect = quantile(data2$indirect, 0.75))
ind.low <- setx(zelig7, indirect = quantile(data2$indirect, 0.25))
out4 <- sim(zelig7, x = ind.low, x1 = ind.high)
summary(out4)

stock.high <- setx(zelig7, indirect = quantile(data2$stock, 0.75))
stock.low <- setx(zelig7, indirect = quantile(data2$stock, 0.25))
out5 <- sim(zelig7, x = stock.low, x1 = stock.high)
summary(out5)

market.high <- setx(zelig7, constraint = quantile(data2$stock, 0.75))
market.low <- setx(zelig7, constraint = quantile(data2$stock, 0.25))
out6 <- sim(zelig7, x = market.low, x1 = market.high)
summary(out6)



